import { Effect, Reducer } from 'umi';
import { getpagedataApi, setpagedataApi } from '@/services/ant-design-pro/api';
export interface IndexModelState {
  data: any[],
  tol: number
}

export interface IndexModelType {
  namespace: 'pages';
  state: IndexModelState;
  effects: {
    query: Effect;
    filters: Effect;
  };
  reducers: {
    save: Reducer<IndexModelState>;
    setNewdata: Reducer<IndexModelState>;
  
    // 启用 immer 之后
    // save: ImmerReducer<IndexModelState>;
  };
}

const IndexModel: IndexModelType = {
  namespace: 'pages',
  state: {
    data: [],
    tol: 0,
  },

  effects: {
    // 初始化
    *query({ payload }, { call, put }) {
      const { data } = yield call(getpagedataApi, payload);
      console.log(data);
      yield put({ type: 'save', payload: data });
    },
    // filter
    
    *filters({ payload }, { call, put }) {
      const { data } = yield call(setpagedataApi, payload);
      yield put({ type: 'setNewdata', payload: data });
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload[0],
        tol: action.payload[1]
      };
    },
    setNewdata(state, action) {
      return {
        ...state,
        data: action.payload[0],
        tol: action.payload[1]
      }
    },
  },
};

export default IndexModel;